set(PROJ_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(SRCS_C "")
set(SRCS_CPP "")
set(SRCS_COMPILE_OPTIONS "-DLV_LVGL_H_INCLUDE_SIMPLE")
set(INCLUDE_DIRS ${PROJ_SRC_DIR})

set(SYSTEM_SPEAKER_SRC_DIR ${PROJ_SRC_DIR})
file(GLOB SYSTEM_SPEAKER_SRCS_C ${SYSTEM_SPEAKER_SRC_DIR}/*.c)
file(GLOB SYSTEM_SPEAKER_SRCS_CPP ${SYSTEM_SPEAKER_SRC_DIR}/*.cpp)
list(APPEND SRCS_C ${SYSTEM_SPEAKER_SRCS_C})
list(APPEND SRCS_CPP ${SYSTEM_SPEAKER_SRCS_CPP})

# App Examples
if(CONFIG_ESP_BROOKESIA_SPEAKER_ENABLE_APP_EXAMPLES)
    list(APPEND SRCS_COMPILE_OPTIONS "-DESP_BROOKESIA_SPEAKER_ENABLE_APP_EXAMPLES=1")
    if(CONFIG_ESP_BROOKESIA_SPEAKER_APP_EXAMPLES_ENABLE_SIMPLE_CONF)
        list(APPEND SRCS_COMPILE_OPTIONS "-DESP_BROOKESIA_SPEAKER_APP_EXAMPLES_ENABLE_SIMPLE_CONF=1")
        file(GLOB_RECURSE SYSTEM_SPEAKER_APP_EXAMPLES_SRCS_C ${SYSTEM_SPEAKER_SRC_DIR}/app_examples/simple_conf/*.c)
        file(GLOB_RECURSE SYSTEM_SPEAKER_APP_EXAMPLES_SRCS_CPP ${SYSTEM_SPEAKER_SRC_DIR}/app_examples/simple_conf/*.cpp)
        list(APPEND SRCS_C ${SYSTEM_SPEAKER_APP_EXAMPLES_SRCS_C})
        list(APPEND SRCS_CPP ${SYSTEM_SPEAKER_APP_EXAMPLES_SRCS_CPP})
    endif()
    if(CONFIG_ESP_BROOKESIA_SPEAKER_APP_EXAMPLES_ENABLE_COMPLEX_CONF)
        list(APPEND SRCS_COMPILE_OPTIONS "-DESP_BROOKESIA_SPEAKER_APP_EXAMPLES_ENABLE_COMPLEX_CONF=1")
        file(GLOB_RECURSE SYSTEM_SPEAKER_APP_EXAMPLES_SRCS_C ${SYSTEM_SPEAKER_SRC_DIR}/app_examples/complex_conf/*.c)
        file(GLOB_RECURSE SYSTEM_SPEAKER_APP_EXAMPLES_SRCS_CPP ${SYSTEM_SPEAKER_SRC_DIR}/app_examples/complex_conf/*.cpp)
        list(APPEND SRCS_C ${SYSTEM_SPEAKER_APP_EXAMPLES_SRCS_C})
        list(APPEND SRCS_CPP ${SYSTEM_SPEAKER_APP_EXAMPLES_SRCS_CPP})
    endif()
    if(CONFIG_ESP_BROOKESIA_SPEAKER_APP_EXAMPLES_ENABLE_SQUARELINE)
        list(APPEND SRCS_COMPILE_OPTIONS "-DESP_BROOKESIA_SPEAKER_APP_EXAMPLES_ENABLE_SQUARELINE=1")
        file(GLOB_RECURSE SYSTEM_SPEAKER_APP_EXAMPLES_SRCS_C ${SYSTEM_SPEAKER_SRC_DIR}/app_examples/squareline/*.c)
        file(GLOB_RECURSE SYSTEM_SPEAKER_APP_EXAMPLES_SRCS_CPP ${SYSTEM_SPEAKER_SRC_DIR}/app_examples/squareline/*.cpp)
        list(APPEND SRCS_C ${SYSTEM_SPEAKER_APP_EXAMPLES_SRCS_C})
        list(APPEND SRCS_CPP ${SYSTEM_SPEAKER_APP_EXAMPLES_SRCS_CPP})
    endif()
endif()

# Widgets
file(GLOB_RECURSE SYSTEM_SPEAKER_WIDGETS_SRCS_C ${SYSTEM_SPEAKER_SRC_DIR}/widgets/*.c)
file(GLOB_RECURSE SYSTEM_SPEAKER_WIDGETS_SRCS_CPP ${SYSTEM_SPEAKER_SRC_DIR}/widgets/*.cpp)
list(APPEND SRCS_C ${SYSTEM_SPEAKER_WIDGETS_SRCS_C})
list(APPEND SRCS_CPP ${SYSTEM_SPEAKER_WIDGETS_SRCS_CPP})

if(ESP_PLATFORM)
    idf_component_register(
        SRCS ${SRCS_C} ${SRCS_CPP}
        INCLUDE_DIRS ${INCLUDE_DIRS}
    )

    set(ASSETS_ANIMATIONS_BOOT "${PROJ_SRC_DIR}/assets/animations/boot")
    spiffs_create_partition_assets(
        anim_boot
        "${ASSETS_ANIMATIONS_BOOT}"
        FLASH_IN_PROJECT
        MMAP_FILE_SUPPORT_FORMAT ".aaf"
        IMPORT_INC_PATH "${ASSETS_ANIMATIONS_BOOT}"
    )

    set(ASSETS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/assets")
    spiffs_create_partition_assets(
        anim_emotion
        "${ASSETS_DIR}/emotion"
        FLASH_IN_PROJECT
        MMAP_FILE_SUPPORT_FORMAT ".aaf"
        IMPORT_INC_PATH "${PROJ_INCLUDE_DIR}"
    )
    spiffs_create_partition_assets(
        anim_icon
        "${ASSETS_DIR}/icon"
        FLASH_IN_PROJECT
        MMAP_FILE_SUPPORT_FORMAT ".aaf"
        IMPORT_INC_PATH "${PROJ_INCLUDE_DIR}"
    )
else()
    set(COMPONENT_LIB esp-brookesia-speaker)
    add_library(${COMPONENT_LIB} STATIC ${SRCS_C} ${SRCS_CPP})
    target_link_libraries(${COMPONENT_LIB} esp-brookesia)
    target_include_directories(${COMPONENT_LIB} PUBLIC ${PROJ_SRC_DIR})
endif()

target_compile_options(${COMPONENT_LIB}
    PUBLIC
        ${SRCS_COMPILE_OPTIONS}

)
if(NOT ESP_PLATFORM)
    target_compile_definitions(${COMPONENT_LIB} PUBLIC ESP_BROOKESIA_KCONFIG_IGNORE)
endif()

set(SRCS_C_COMPILE_FLAGS "-Wno-format")
set(SRCS_CPP_COMPILE_FLAGS "-Wno-missing-field-initializers -Wno-format")
set_source_files_properties(
    ${SRCS_CPP}
    PROPERTIES
        COMPILE_FLAGS "${SRCS_CPP_COMPILE_FLAGS}"
)
set_source_files_properties(
    ${SRCS_C}
    PROPERTIES
        COMPILE_FLAGS "${SRCS_C_COMPILE_FLAGS}"
)
